// #include "esp_wifi.h"
// #include "driver/gpio.h"
// #include "freertos/event_groups.h"
// #include "esp_wifi.h"
// #include "esp_event.h"
// #include "nvs.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
// #include <lwip/netdb.h>
#include <stdio.h>
#include "selftest_mode.h"
#include "nvs_value.h"
#include "main.h"
#include "radar.h"
#include "generic_gpio.h"
#include "Analog_IIC.h"
#include "lamp_ctrl.h"
#include "touch_board.h"
#include "ws2812.h"

static const char *TAG = "[TEST]";

#define TASK_BASE_TICK_MS (100)

uint8_t selftest_iic_ret = 2;

static void selftest_task(void *parm)
{
	ESP_LOGI(TAG,"selftest start!!!");
	uint8_t self_test_cnt = 0;
	uint8_t sec_count = 0;
	bool change = 0;
	uint8_t code = 0;
	device_attr.debug_mode = 7;
	uint8_t key_bit = 0;
	uint8_t color_temp_dir = 0;

	while(1){
		if(self_test_cnt >= 5){
			sec_count++;
			self_test_cnt = 0;
			change = !change;
			key_bit = 31*change;
			color_temp_dir = 255*change;
			ET6226M_write_data(DIS_ADD_4, key_bit);
		}
		switch(system_state.selftest){
			case START:
				printf("->[OK]start\n");
				set_slide_indicator(16);
				vTaskDelay(500 / portTICK_PERIOD_MS);
				printf("->[OK]step1:%s\n", device_attr.device_mac);
				set_slide_indicator(48);
				system_state.selftest = IIC_TEST;
				break;
			case IIC_TEST:
				if(selftest_iic_ret == 0){
					system_state.selftest = WIFI_TEST;
					printf("->[OK]step2:indicator\n");
					set_slide_indicator(80);
					code |= 1<<0;
				}else if(selftest_iic_ret == 1){
					printf("->[ERROR]step2:indicator\n");
					system_state.selftest = END;
				}
				break;
			case WIFI_TEST:
				system_state.selftest = RADAR_TEST;
				printf("->[OK]step3:wireless\n");
				code |= 1<<1;
				set_slide_indicator(112);
				break;
			case RADAR_TEST:
				if(1){
					system_state.selftest = PWM_TEST;
					printf("->[OK]step4:radar\n");
					set_slide_indicator(144);
					code |= 1<<2;
					printf("->[WAIT]step5:lamp\n");
					set_slide_indicator(176);
				}else if(sec_count >= 60){
					system_state.selftest = PWM_TEST;
					printf("->[ERROR]step4:radar\n");
					system_state.selftest = END;
				}
				break;
			case PWM_TEST:
				if(top_led_mode == 2){
					system_state.selftest = REPORT;
					printf("->[OK]step6:lamp\n");
					set_slide_indicator(208);
					code |= 1<<3;
				}else if(sec_count/4){
					lamp_set_fade(90, color_temp_dir, 200);
				}
				break;
			case REPORT:
				if(code == 0x0f){
					printf("->[PASS]%s\n", device_attr.device_mac);
					set_slide_indicator(240);
					system_state.selftest = END;
				}else{
					printf("->[FAIL]errorCode:%d\n", code);
					set_slide_indicator(0);
					system_state.selftest = END;
				}
				break;
			case END:
				while(system_state.selftest == END){
					vTaskDelay(500 / portTICK_PERIOD_MS);
				}
				break;
			default:
				break;
		}
		self_test_cnt++;
		vTaskDelay(TASK_BASE_TICK_MS / portTICK_PERIOD_MS);
	}
}

void selftest_init(void)
{
	ESP_LOGI(TAG,"selftest_init");
	xTaskCreate(selftest_task, "selftest_task", 2048, NULL, 4, NULL);
}
